// pages/test-detail/index.js
const $http = require('../../request/index');

Page({

  /** 
   * 页面的初始数据
   */
  data: {
    // 动态绑定 确认按钮的提示信息 最后一题显示为提交
    confirmDescWord: "继续",
    // 该组卷的id 用于核对
    examId: "",
    // 看该试卷练习是否是 练习还是试卷
    type: "",
    // 第几题
    questionIndex: 0,
    // 共有几道题
    totalExamCount: 0,
    // 用户的选择
    userChoice: [],
    // 当前题号
    currentQuestionId: "",
    // 题目+答案集合
    allQuestionsMessage: {},
    // 之前题号 用于上一页的访问
    lastQuestionId: "",
    // 按钮是否可以点击 选下选项后为true  取消为false
    disable: false,
    // ----------------------------------------------------------
    // 题目集
    remessage: [],
    time: '30',
    // 在试卷提交后 所有id 和 选择的内容都会在这里呈现
    questionCheckQuery: {
      id: [],
      selectedAnswer: []
    },
  },

  onLoad: function (options) {
    // 设置 试卷的id  
    this.setData({
      examId: options.id,
      type: options.type
    })
    this.getExamDetail();
    this.initTime();
  },
  onShow: function (options) {

  },
  // 多选 用includes 考虑
  // 点击选项时候
  answer: function (e) {
    let type = e.currentTarget.dataset.type
    let choice = e.currentTarget.dataset.index;
    let examId = e.currentTarget.dataset.uid
    //（type =1 =>单选）数组中仅有一个元素 点了第一个 再点第二个 会清除第一个
    if (type == 1) {
      // 重复点击取消
      if (this.data.userChoice.indexOf(choice) != -1) { //找到了 该元素
        this.setData({
          userChoice: [],
          currentQuestionId: examId,
          disable: false,
        })
      } else {
        // 点击 再选中
        let tmp = [];
        tmp.push(choice)
        this.setData({
          userChoice: tmp,
          currentQuestionId: examId,
          disable: true,
        })
        console.log("userChoice", this.data.userChoice)
      }
    } else {
      // // 多选情况 先选择第一个 保存下来
      // // 1. 拿到之前选择的信息
      let tmp = this.data.userChoice;
      // // 2.加入现在的选择的信息
      // 如果存在就删去 如果不存在就
      if (tmp.includes(choice)) {
        let index = tmp.indexOf(choice)
        tmp.splice(index, 1);
      } else {
        tmp.push(choice);
      }
      let flag
      if (tmp.length == 0)
        // 多选 如果没有选择内容的话 我就禁用按钮
        flag = false;
      else flag = true;
      this.setData({
        currentQuestionId: examId,
        userChoice: tmp,
        disable: flag,
      })
    }

  },
  // 试卷提交
  toConfirm() {
    let questionId = this.data.currentQuestionId
    // 题号++ 
    let cnt1 = this.data.questionIndex;
    // 拿到之前的值
    let currentQuestionList = this.data.allQuestionsMessage;
    //  放入到容器当中
    currentQuestionList[questionId] = this.data.userChoice
    // 点击后 到下一题 清空当前用户选择 保存用户的当前选择记录
    let that = this
    this.setData({
      questionIndex: ++cnt1,
      userChoice: [],
      allQuestionsMessage: currentQuestionList,
      disable: false,
    })
    console.log("dddd", this.data.allQuestionsMessage)
    //倒数第一题提交
    if (that.data.questionIndex == that.data.remessage.length) {
      $http.request("POST", `/front/exam/do`, {
        examId: this.data.examId,
        answers: this.data.allQuestionsMessage
      }).then(res => {
        // console.log("试卷提交Res", res)
        let { data } = res.data
        // 全局变量可能会改变 但是localstrage
        wx.setStorageSync("lastCheckAnswer", data);
        wx.navigateBack({
          delta: 1,
          success: (res => {
            wx.showToast({
              title: '提交成功',
              icon: 'success',
              duration: 1500
            })
          })
        })
      })

    }
  },

  // 获取组卷信息
  getExamDetail() {
    $http.request("GET", `/front/exam/${this.data.examId}`, "").then(res => {
      console.log("res", res)
      let { data } = res.data;
      this.setData({
        remessage: data.questions,
        totalExamCount: data.questions.length
      })
    })
  },
  initTime() {
    wx.setNavigationBarTitle({
      title: "反诈练习"
    })
    var that = this;
    let timer = setInterval(function () {
      if (that.data.type == "试卷") {
        that.setData({  //不断-1
          time: that.data.time - 1,
        })
      } else {
        that.setData({
          time: "∞",
        })
      }
      if (that.data.time != "∞") {
        if (that.data.time <= 0) { //当时间结束的时候 记录答对情况  
          clearInterval(timer);
          wx.navigateBack({
            delta: 1
          })
        }
      }
    }, 1000)
  },

  previousPage(event) {
    let questionId = this.data.lastQuestionId
    // 题号++ 
    let cnt1 = this.data.questionIndex;
    // 拿到之前的值
    let currentQuestionList = this.data.allQuestionsMessage;
    let tmp = currentQuestionList[questionId]
    // 点击后 到下一题 清空当前用户选择 保存用户的当前选择记录
    // 题号-1  重新显示选择
    this.setData({
      questionIndex: --cnt1,
      userChoice: tmp,
      disable: true
    })
  },

  /**
   * 上一页的处理逻辑
   * 第一道题时 last为空, 当我点击下一题 跳转到第二题时 last 保存第一题的id 点击上一题时 拿到id 去找用户的答题信息里 有没有我想要的
   *  
   */
  // 下一页
  nextPage(event) {
    let questionId = this.data.currentQuestionId
    // 题号++ 
    let cnt1 = this.data.questionIndex;
    // 拿到之前的答案和问题
    let currentQuestionList = this.data.allQuestionsMessage;
    //  将现在用户选择的题号存入数组中
    currentQuestionList[questionId] = this.data.userChoice
    // 点击后 到下一题 清空当前用户选择 保存用户的当前选择记录
    this.setData({
      questionIndex: ++cnt1,
      userChoice: [],
      allQuestionsMessage: currentQuestionList,
      disable: false,
      lastQuestionId: questionId
    })

  },
  // 查看答案按钮 跳转到查看答案的界面
  openIntoCheckAnswer() {
    let questionId = this.data.currentQuestionId
    // 题号++ 
    let cnt1 = this.data.questionIndex;
    // 拿到之前的值
    let currentQuestionList = this.data.allQuestionsMessage;
    //  放入到容器当中
    currentQuestionList[questionId] = this.data.userChoice
    // 点击后 到下一题 清空当前用户选择 保存用户的当前选择记录
    let that = this
    this.setData({
      questionIndex: ++cnt1,
      userChoice: [],
      allQuestionsMessage: currentQuestionList,
      disable: false,
    })
    $http.request("POST", `/front/exam/do`, {
      examId: this.data.examId,
      answers: this.data.allQuestionsMessage
    }).then(res => {
      console.log("试卷提交Res", res)
      let { data } = res.data
      //  存数据 并跳转到 查看答案的界面 并注明这是一个练习
      wx.setStorageSync("lastCheckAnswer", data);
      wx.redirectTo({
        url: `/pages/checkAnswer/index?type="练习"&id=${this.data.examId}`,
      })
    })
  }

})